/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
fnCommon.fnCheckLogin( MOD_LOGIN );

var fnLoginModel = function(){
    //PUBLIC FUNCTIONS
    function fnLogin(){
//        console.log( "[fnLogin] << ENTER" )
        var oResponse;
        var oParams;
        var u = $( "#txt_usuario" ).val();
        var p = $( "#txt_password" ).val();
        
        oParams ={
            accion : ACC_LOGIN,
            u      : u,
            p      : p
        }
        
        oResponse = fnCommon.fnSendServer( oParams, CONTROLLER_LOGIN , JSON_FORMAT );
        if( oResponse !=  null ) {
            switch( parseInt( oResponse.code ) ){
                case CODE_SUCCESS:
                    fnCommon.fnRedirec();
                    break;
                case CODE_FAILED:
                    break;
                case EC_USER_NOT_REGISTERED:
                case EC_USER_OR_PASSWORD_INCORRECTS:
                    fnCommon.fnShowInfoMessage( "Datos incorrectos." , CLS_MSG_ERROR, "#msg_login" );
                    $( "#txt_usuario" ).addClass( "error" );
                    $( "#txt_password" ).addClass( "error" );
                    break;
            }
        }
//        console.log( "[fnLogin] >> EXIT" );
        return false;
    }

    function fnInitValidForms(){
//        console.log( "[fnInitValidForms] << ENTER" );
        $("#frm_login").validate({
            errorLabelContainer:"#msg_login",
            submitHandler: function(form) {
                fnLogin();
            },
            rules: {
                // simple rule, converted to {required:true}
                txt_usuario: "required",
                // compound rule
                txt_password: "required"
            },
            messages:{
                txt_usuario:{
                    required:""
                },
                txt_password:{
                    required:""
                }
            }
        
        });
//        console.log( "[fnInitValidForms] >> EXIT" );
    }
    return{
        fnInitValidForms : fnInitValidForms,
        fnLogin          : fnLogin
    }
}();
